﻿using System;
using System.Xml;
using Google.GData.Spreadsheets;

//[assembly:CLSCompliant(true)]

namespace uk.org.aspellclark.todolistppc
{
    /// <summary>
    ///   <name>CTask</name>
    ///   <namespace>uk.org.aspellclark.todolistppc</namespace>
    ///   <version>1.0</version>
    ///   <author>Andy Aspell-Clark</author>
    ///   <description>
    ///   </description>
    ///   <history>
    ///     <historyitem> 1 Aug 2004  1.0 ARA  Initial Version.</historyitem>
    ///   </history>
    /// </summary>
    public class SyncTask
    {
        #region Member variables

        private string m_OutlookAppointmentID = "";
        private bool m_bComplete;
        private Decimal _mDTimeSpent = 5;
        private int _mIParentTaskId;
        private Int16 m_iPercentComplete;
        private Int16 m_iPosition;
        private Int16 m_iPriority = 5;
        private Int64 m_iPriorityColour = 5;
        private Int16 m_iRisk = 5;
        private int m_iTaskId;
        private Int16 m_iTimeEstimate = 5;

        private string m_strCost;
        private string m_strDependancy;
        private string m_strExternalId;
        private string m_strFileLink;
        private string m_strFlag;

        #region Times

        private DateTime m_dtCompletedDate;

        private DateTime m_dtCreationDate;

        private DateTime m_dtDoneDate;
        private DateTime m_dtDueDate;

        private DateTime m_dtLastMod;
        private DateTime m_dtStartDate;
        private string m_sTimeEstUnits = "H";
        private string m_sTimeSpentUnits = "H";
        private string m_strCompletedDate;
        private string m_strCreationDate;
        private string m_strDoneDate;
        private string m_strDueDate;
        private string m_strStartDate;
        private string m_strm_dtLastMod;

        #endregion

        #endregion

        public SyncTask()
        {
        }

        public SyncTask(ListEntry GoogleRow)
        {
            TaskId = Convert.ToInt32(GoogleRow.Elements[0].Value);
            Title = GoogleRow.Elements[1].Value;
            if (GoogleRow.Elements[3].Value == "0")
            {
                Complete = false;
            }
            else
            {
                Complete = true;
            }
            //Task.DoneDate = GoogleRow.Elements[4].Value;
            CommentsStr = GoogleRow.Elements[5].Value;

            var epoch = new DateTime(1970, 1, 1);
            LastMod = epoch.AddSeconds(double.Parse(GoogleRow.Elements[12].Value.Substring(0, 10)));


            //Task.DueDate = GoogleRow.Elements[7].Value;
            ParentTaskId = Convert.ToInt32(GoogleRow.Elements[19].Value);
        }

        #region Properties

        public String AllocatedBy { get; set; }

        public String AllocatedTo { get; set; }

        public String AllocatedTo1 { get; set; }

        public String Category { get; set; }

        public Int64 Colour { get; set; }

        public String CommentsType { get; set; }

        public Boolean Complete
        {
            get { return m_bComplete; }
            set
            {
                m_bComplete = value;
                if (value)
                {
                    m_iPercentComplete = 100;
                }
                else
                {
                    m_iPercentComplete = 0;
                }
            }
        }

        public String CreatedBy { get; set; }

        public DateTime CreationDate
        {
            get { return m_dtCreationDate; }
            set
            {
                m_dtCreationDate = value;
                m_strCreationDate = FormatDateString(value.ToString("yyyy-mm-dd"));
            }
        }

        public DateTime DoneDate
        {
            get { return m_dtDoneDate; }
            set
            {
                m_dtDoneDate = value;
                m_strDoneDate = FormatDateString(value.ToString("yyyy-mm-dd"));
            }
        }

        public string DoneDateStr
        {
            get { return m_dtDoneDate.ToString("yyyy-mm-dd"); }
        }

        public DateTime DueDate
        {
            get { return m_dtDueDate; }
            set
            {
                m_dtDueDate = value;
                m_strDueDate = FormatDateString(value.ToString("yyyy-mm-dd"));
            }
        }

        public string DueDateStr
        {
            get { return m_dtDueDate.ToString("yyyy-mm-dd"); }
        }

        public String FileRefPath { get; set; }

        public Single HoursEst { get; set; }

        public String CommentsStr { get; set; }

        public int ParentTaskId
        {
            get { return _mIParentTaskId; }
            set
            {
                if (value >= 0 && value <= 16384)
                {
                    _mIParentTaskId = value;
                }
            }
        }

        public Int16 PercentComplete
        {
            get { return m_iPercentComplete; }
            set
            {
                if (value >= 0 && value <= 100)
                {
                    m_iPercentComplete = value;
                    if (value == 100)
                    {
                        m_bComplete = true;
                    }
                    else
                    {
                        m_bComplete = false;
                    }
                }
            }
        }

        public Int16 Position
        {
            get { return m_iPosition; }
            set
            {
                if (value >= 0 && value <= 100)
                {
                    m_iPosition = value;
                }
            }
        }

        public Int16 Priority
        {
            get { return m_iPriority; }
            set
            {
                if (value >= 0 && value <= 10)
                {
                    m_iPriority = value;
                }
            }
        }

        public Int64 PriorityColour
        {
            get { return m_iPriorityColour; }
            set { m_iPriorityColour = value; }
        }

        public string PriorityWebColour { get; set; }

        public Int16 Risk
        {
            get { return m_iRisk; }
            set { m_iRisk = value; }
        }

        public string RtfComments { get; set; }

        public DateTime StartDate
        {
            get { return m_dtStartDate; }
            set
            {
                m_dtStartDate = value;
                m_strStartDate = FormatDateString(value.ToString("yyyy-mm-dd"));
            }
        }

        public string StartDateStr
        {
            get { return m_dtStartDate.ToString("yyyy-mm-dd"); }
        }

        public String Status { get; set; }

        public int TaskId
        {
            get { return m_iTaskId; }
            set
            {
                if (value >= 0 && value <= 16384)
                {
                    m_iTaskId = value;
                }
            }
        }

        public String TextColour { get; set; }

        public String TextWebColour { get; set; }

        public String TimeEstUnits { get; set; }

        public String TimeESpentUnits { get; set; }

        public Decimal TimeSpent
        {
            get { return _mDTimeSpent; }
            set { _mDTimeSpent = value; }
        }

        public String Title { get; set; }

        public String VersionStr { get; set; }

        public String WebColour { get; set; }

        public DateTime LastMod
        {
            get { return m_dtLastMod; }
            set
            {
                m_dtLastMod = value;
                m_strm_dtLastMod = FormatDateString(value.ToString("yyyy-mm-dd"));
            }
        }

        public string OutlookAppointmentID
        {
            get { return m_OutlookAppointmentID; }
            set { m_OutlookAppointmentID = value; }
        }

        public bool OutlookAppointmentEnabled { get; set; }

        #endregion

        private String FormatDateString(String strDate)
        {
            String strReturn;

            try
            {
                strReturn = Convert.ToDateTime(strDate).ToString("MM/dd/yyyy");
            }
            catch
            {
                strReturn = strDate;
            }

            return strReturn;
        }


        /// <summary>
        /// Reads in this tasks properties from the xml node
        /// </summary>
        /// <param name="xndTask"></param>
        public void readFromXml(XmlNode xndTask)
        {
            try
            {
                if (xndTask.Attributes.GetNamedItem("ALLOCATEDBY") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("ALLOCATEDBY").InnerText.Length > 0)
                    {
                        AllocatedBy = xndTask.Attributes.GetNamedItem("ALLOCATEDBY").InnerText;
                    }
                }
                if (xndTask.Attributes.GetNamedItem("CATEGORY") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("CATEGORY").InnerText.Length > 0)
                    {
                        Category = xndTask.Attributes.GetNamedItem("CATEGORY").InnerText;
                    }
                }
                if (xndTask.Attributes.GetNamedItem("COLOUR") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("COLOUR").InnerText.Length > 0)
                    {
                        Colour = Convert.ToInt64(xndTask.Attributes.GetNamedItem("COLOUR").InnerText);
                    }
                }
                if (xndTask.Attributes.GetNamedItem("COMMENTS") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("COMMENTS").InnerText.Length > 0)
                    {
                        CommentsStr = xndTask.Attributes.GetNamedItem("COMMENTS").InnerText;
                    }
                }
                if (xndTask.Attributes.GetNamedItem("COMMENTSTYPE") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("COMMENTSTYPE").InnerText.Length > 0)
                    {
                        CommentsType = xndTask.Attributes.GetNamedItem("COMMENTSTYPE").InnerText;
                    }
                }
                if (xndTask.Attributes.GetNamedItem("CREATIONDATE") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("CREATIONDATE").InnerText.Length > 0)
                    {
                        CreationDate =
                            DateTime.FromOADate(
                                Convert.ToDouble(xndTask.Attributes.GetNamedItem("CREATIONDATE").InnerText));
                    }
                }
                if (xndTask.Attributes.GetNamedItem("CUSTOMCOMMENTS") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("CUSTOMCOMMENTS").InnerText.Length > 0)
                    {
                        RtfComments = xndTask.Attributes.GetNamedItem("CUSTOMCOMMENTS").InnerText;
                    }
                }
                if (xndTask.Attributes.GetNamedItem("DUEDATE") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("DUEDATE").InnerText.Length > 0)
                    {
                        DueDate =
                            DateTime.FromOADate(Convert.ToDouble(xndTask.Attributes.GetNamedItem("DUEDATE").InnerText));
                    }
                }
                if (xndTask.Attributes.GetNamedItem("DONEDATE") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("DONEDATE").InnerText.Length > 0)
                    {
                        DoneDate =
                            DateTime.FromOADate(Convert.ToDouble(xndTask.Attributes.GetNamedItem("DONEDATE").InnerText));
                    }
                }
                if (xndTask.Attributes.GetNamedItem("FILEREFPATH") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("FILEREFPATH").InnerText.Length > 0)
                    {
                        FileRefPath = xndTask.Attributes.GetNamedItem("FILEREFPATH").InnerText;
                    }
                }
                if (xndTask.Attributes.GetNamedItem("ID") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("ID").InnerText.Length > 0)
                    {
                        TaskId = Convert.ToInt16(xndTask.Attributes.GetNamedItem("ID").InnerText);
                    }
                }
                if (xndTask.Attributes.GetNamedItem("LASTMOD") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("LASTMOD").InnerText.Length > 0)
                    {
                        LastMod =
                            DateTime.FromOADate(Convert.ToDouble(xndTask.Attributes.GetNamedItem("LASTMOD").InnerText));
                    }
                }

                Title = xndTask.Attributes.GetNamedItem("TITLE").InnerText;

                if (xndTask.Attributes.GetNamedItem("PERCENTDONE") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("PERCENTDONE").InnerText.Length > 0)
                    {
                        PercentComplete = Convert.ToInt16(xndTask.Attributes.GetNamedItem("PERCENTDONE").InnerText);
                    }
                }
                if (xndTask.Attributes.GetNamedItem("PERSON") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("PERSON").InnerText.Length > 0)
                    {
                        AllocatedTo = xndTask.Attributes.GetNamedItem("PERSON").InnerText;
                    }
                }
                if (xndTask.Attributes.GetNamedItem("POS") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("POS").InnerText.Length > 0)
                    {
                        Position = Convert.ToInt16(xndTask.Attributes.GetNamedItem("POS").InnerText);
                    }
                }
                if (xndTask.Attributes.GetNamedItem("PRIORITY") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("PRIORITY").InnerText.Length > 0)
                    {
                        Priority = Convert.ToInt16(xndTask.Attributes.GetNamedItem("PRIORITY").InnerText);
                    }
                }
                if (xndTask.Attributes.GetNamedItem("PRIORITYCOLOR") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("PRIORITYCOLOR").InnerText.Length > 0)
                    {
                        PriorityColour = Convert.ToInt64(xndTask.Attributes.GetNamedItem("PRIORITYCOLOR").InnerText);
                    }
                }
                if (xndTask.Attributes.GetNamedItem("PRIORITYWEBCOLOR") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("PRIORITYWEBCOLOR").InnerText.Length > 0)
                    {
                        PriorityWebColour = xndTask.Attributes.GetNamedItem("PRIORITYWEBCOLOR").InnerText;
                    }
                }
                if (xndTask.Attributes.GetNamedItem("RISK") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("RISK").InnerText.Length > 0)
                    {
                        Risk = Convert.ToInt16(xndTask.Attributes.GetNamedItem("RISK").InnerText);
                    }
                }
                if (xndTask.Attributes.GetNamedItem("STARTDATE") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("STARTDATE").InnerText.Length > 0)
                    {
                        StartDate =
                            DateTime.FromOADate(Convert.ToDouble(xndTask.Attributes.GetNamedItem("STARTDATE").InnerText));
                    }
                }
                if (xndTask.Attributes.GetNamedItem("STATUS") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("STATUS").InnerText.Length > 0)
                    {
                        Status = xndTask.Attributes.GetNamedItem("STATUS").InnerText;
                    }
                }
                if (xndTask.Attributes.GetNamedItem("TEXTCOLOUR") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("TEXTCOLOUR").InnerText.Length > 0)
                    {
                        TextColour = xndTask.Attributes.GetNamedItem("TEXTCOLOUR").InnerText;
                    }
                }
                if (xndTask.Attributes.GetNamedItem("TEXTWEBCOLOUR") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("TEXTWEBCOLOUR").InnerText.Length > 0)
                    {
                        TextWebColour = xndTask.Attributes.GetNamedItem("TEXTWEBCOLOUR").InnerText;
                    }
                }
                if (xndTask.Attributes.GetNamedItem("TIMEESTIMATE") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("TIMEESTIMATE").InnerText.Length > 0)
                    {
                        HoursEst = Convert.ToSingle(xndTask.Attributes.GetNamedItem("TIMEESTIMATE").InnerText);
                    }
                }
                if (xndTask.Attributes.GetNamedItem("TIMESPENT") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("TIMESPENT").InnerText.Length > 0)
                    {
                        TimeSpent = Convert.ToDecimal(xndTask.Attributes.GetNamedItem("TIMESPENT").InnerText);
                    }
                }
                if (xndTask.Attributes.GetNamedItem("VERSION") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("VERSION").InnerText.Length > 0)
                    {
                        VersionStr = xndTask.Attributes.GetNamedItem("VERSION").InnerText;
                    }
                }
                if (xndTask.Attributes.GetNamedItem("WEBCOLOUR") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("WEBCOLOUR").InnerText.Length > 0)
                    {
                        WebColour = xndTask.Attributes.GetNamedItem("WEBCOLOUR").InnerText;
                    }
                }
                if (xndTask.Attributes.GetNamedItem("OUTLOOKAPPOINTMENTID") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("OUTLOOKAPPOINTMENTID").InnerText.Length > 0)
                    {
                        OutlookAppointmentID = xndTask.Attributes.GetNamedItem("OUTLOOKAPPOINTMENTID").InnerText;
                    }
                }
                if (xndTask.Attributes.GetNamedItem("OUTLOOKAPPOINTMENTENABLED") != null)
                {
                    if (xndTask.Attributes.GetNamedItem("OUTLOOKAPPOINTMENTENABLED").InnerText.Length > 0)
                    {
                        OutlookAppointmentEnabled =
                            Convert.ToBoolean(xndTask.Attributes.GetNamedItem("OUTLOOKAPPOINTMENTENABLED").InnerText);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception reading todolist [" + e.Message + "]");
            }

            if (PercentComplete == 100)
            {
                Complete = true;
            }
        }

//readFromXml()

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlWriter"></param>
        public void writeToXml(XmlTextWriter xmlWriter)
        {
            xmlWriter.WriteStartElement("TASK");

            xmlWriter.WriteAttributeString("", "ID", "", TaskId.ToString());
            DateTime lastModded = LastMod;
            xmlWriter.WriteAttributeString("", "LASTMODSTRING", "", lastModded.ToString("yyyy-mm-dd"));
            xmlWriter.WriteAttributeString("", "LASTMOD", "", lastModded.ToOADate().ToString());

            if (!string.IsNullOrEmpty(AllocatedBy))
            {
                xmlWriter.WriteAttributeString("", "ALLOCATEDBY", "", AllocatedBy);
            }
            if (!string.IsNullOrEmpty(AllocatedTo) && AllocatedTo.Length > 0)
            {
                xmlWriter.WriteAttributeString("", "PERSON", "", AllocatedTo);
            }
            if (!string.IsNullOrEmpty(AllocatedTo1) && AllocatedTo1.Length > 0)
            {
                xmlWriter.WriteAttributeString("", "PERSON1", "", AllocatedTo);
            }
            if (!string.IsNullOrEmpty(Category))
            {
                xmlWriter.WriteAttributeString("", "CATEGORY", "", Category);
            }

            xmlWriter.WriteAttributeString("", "COLOR", "", Colour.ToString());

            if (!string.IsNullOrEmpty(CommentsStr))
            {
                xmlWriter.WriteAttributeString("", "COMMENTS", "", CommentsStr);
            }
            if (!string.IsNullOrEmpty(CommentsType))
            {
                xmlWriter.WriteAttributeString("", "COMMENTSTYPE", "", CommentsType);
            }
            if (!string.IsNullOrEmpty(CreatedBy))
            {
                xmlWriter.WriteAttributeString("", "CREATEDBY", "", CreatedBy);
            }
            if (!string.IsNullOrEmpty(m_strCreationDate))
            {
                xmlWriter.WriteAttributeString("", "CREATIONDATESTRING", "", CreationDate.ToString("yyyy-mm-dd"));
                xmlWriter.WriteAttributeString("", "CREATIONDATE", "", CreationDate.ToString());
            }
            if (!string.IsNullOrEmpty(m_strDueDate))
            {
                xmlWriter.WriteAttributeString("", "DUEDATESTRING", "", DueDate.ToString("yyyy-mm-dd"));
                xmlWriter.WriteAttributeString("", "DUEDATE", "", DueDate.ToString());
            }
            if (!string.IsNullOrEmpty(m_strDoneDate))
            {
                xmlWriter.WriteAttributeString("", "DONEDATESTRING", "", DoneDate.ToString("yyyy-mm-dd"));
                xmlWriter.WriteAttributeString("", "DONEDATE", "", DoneDate.ToString());
            }
            if (PercentComplete > -1)
            {
                xmlWriter.WriteAttributeString("", "PERCENTDONE", "", PercentComplete.ToString());
            }
            if (Position > 0)
            {
                xmlWriter.WriteAttributeString("", "POS", "", Position.ToString());
            }
            xmlWriter.WriteAttributeString("", "PRIORITY", "", Priority.ToString());
            xmlWriter.WriteAttributeString("", "PRIORITYCOLOR", "", PriorityColour.ToString());
            if (!string.IsNullOrEmpty(PriorityWebColour))
            {
                xmlWriter.WriteAttributeString("", "PRIORITYWEBCOLOR", "", PriorityWebColour);
            }
            if (Risk > 0)
            {
                xmlWriter.WriteAttributeString("", "RISK", "", Risk.ToString());
            }
            if (!string.IsNullOrEmpty(m_strStartDate))
            {
                xmlWriter.WriteAttributeString("", "STARTDATESTRING", "", StartDate.ToString("yyyy-mm-dd"));
                xmlWriter.WriteAttributeString("", "STARTDATE", "", StartDate.ToString());
            }
            if (!string.IsNullOrEmpty(Status))
            {
                xmlWriter.WriteAttributeString("", "STATUS", "", Status);
            }
            else
            {
                xmlWriter.WriteAttributeString("", "STATUS", "", "todo");
            }
            if (!string.IsNullOrEmpty(TextColour))
            {
                xmlWriter.WriteAttributeString("", "TEXTCOLOR", "", TextColour);
            }
            if (!string.IsNullOrEmpty(TextWebColour))
            {
                xmlWriter.WriteAttributeString("", "TEXTWEBCOLOR", "", TextWebColour);
            }
            if (HoursEst > -1)
            {
                xmlWriter.WriteAttributeString("", "TIMEESTIMATE", "", HoursEst.ToString());
            }
            if (!string.IsNullOrEmpty(TimeESpentUnits))
            {
                xmlWriter.WriteAttributeString("", "TIMEESPENTUNITS", "", TimeESpentUnits);
            }
            if (!string.IsNullOrEmpty(TimeEstUnits))
            {
                xmlWriter.WriteAttributeString("", "TIMEESTUNITS", "", TimeEstUnits);
            }
            xmlWriter.WriteAttributeString("", "TIMESPENT", "", TimeSpent.ToString());


            xmlWriter.WriteAttributeString("", "TITLE", "", Title);
            if (!string.IsNullOrEmpty(VersionStr))
            {
                xmlWriter.WriteAttributeString("", "VERSION", "", VersionStr);
            }
            if (!string.IsNullOrEmpty(WebColour))
            {
                xmlWriter.WriteAttributeString("", "WEBCOLOR", "", WebColour);
            }
            if (!string.IsNullOrEmpty(OutlookAppointmentID))
            {
                xmlWriter.WriteAttributeString("OUTLOOKAPPOINTMENTID", OutlookAppointmentID);
            }
            xmlWriter.WriteAttributeString("", "WEBCOLOR", "", OutlookAppointmentEnabled.ToString());
        }

        /// <summary>
        /// 
        /// </summary>
        public void endWriteToXml(XmlTextWriter xmlWriter)
        {
            xmlWriter.WriteEndElement();
        }
    }

//class()
}

//namespace()